#!/bin/bash

#	Copyright 2010 David "Alemarius Nexus" Lerch
#
#	This file is part of gtatxd.
#
#	gtatxd is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#
#	gtatxd is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with gtatxd.  If not, see <http://www.gnu.org/licenses/>.

if [ ! -f "$1" ]
then
	echo Error: Input file \'$1\' does not exist!
	exit 1
fi

if [ ! -d "$2" ]
then
  echo Error: Destination directory \'$2\' does not exist!
  exit 1
fi

ovw="$2/status.html"

echo '<?xml version="1.0"?>' > $ovw
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
' >> $ovw

echo "<!--
  This document was auto-generated by the GTA texture extraction test script.
-->" >> $ovw

echo "<html>" >> $ovw
echo "  <head>" >> $ovw
echo "    <title>Texture Conversion Report</title>" >> $ovw
echo "  </head>" >> $ovw
echo "  <body>" >> $ovw
echo "    <h1>Texture Conversion Report</h1>" >> $ovw

texdataFile=`mktemp`

declare -A formatCounts
declare -A compressionCounts

echo >> $ovw
echo "    <h2>Details</h2>" >> $ovw

echo '    <table border="1">' >> $ovw
echo '      <tr bgcolor="#4682B4">' >> $ovw
echo '        <th>TXD File</th>' >> $ovw
echo '      </tr>' >> $ovw

txdfiles="`gtaimg ls "$1" | grep '\.txd'`"
numtxds="`echo "$txdfiles" | wc -l`"
txdcount="0"

for txdfile in `gtaimg ls "$1" | grep '\.txd'`
#for txdfile in `gtaimg ls "$1" | grep 'camera.*'`
do
	if [ $? -ne 0 ]
		then
		echo "*** Error 0, aborting."
		exit $?
	fi

	tmpTxdfile=`mktemp`
	gtaimg x "$1" "$txdfile" "$tmpTxdfile" > /dev/null

	if [ $? -ne 0 ]
		then
		echo "*** Error 1, aborting."
		exit $?
	fi

	gtatxd ls -s -c -w -h -a -b -f "$tmpTxdfile" | tr ' ' '%' > "$texdataFile"
	
	if [ $? -ne 0 ]
		then
		echo "*** Error 2, aborting."
		exit $?
	fi

    txdcount="$(($txdcount + 1))"


	if [ -n "`tr -d ' \r\n\t' < "$texdataFile"`" ]
	then
		destdir=`echo "$txdfile" | awk '{print $1}'`
		
		i=1
		while [ -d "$2/$destdir" ]
		do
			destdir="$destdir%$(( ++i ))"
		done

		echo "Extracting textures in '$txdfile' ($txdcount/$numtxds)..."
		mkdir "$2/$destdir"
		
		txdOvw="$2/$destdir/status.html"
		
		echo '      <tr bgcolor="#00BFFF">' >> $ovw
		echo "        <td><a href=\"$destdir/status.html\">$txdfile</a></td>" >> $ovw
		echo '      </tr>' >> $ovw
		
		echo '<?xml version="1.0"?>' > "$txdOvw"
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	    	   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
		' >> "$txdOvw"
		
		echo '<html>' >> "$txdOvw"
		echo '  <head>' >> "$txdOvw"
		echo "    <title>$txdfile Status Overview</title>" >> "$txdOvw"
		echo '  </head>' >> "$txdOvw"
		echo '  <body>' >> "$txdOvw"

		echo "    <h1>$txdfile Status Overview</h1>" >> "$txdOvw"
		echo '    <table border="1" width="100%">' >> "$txdOvw"
		echo '      <tr bgcolor="#4682B4">' >> "$txdOvw"
		echo "        <th>Texture</th>" >> "$txdOvw"
		echo "        <th>Compression</th>" >> "$txdOvw"
		echo "        <th>Width</th>" >> "$txdOvw"
		echo "        <th>Height</th>" >> "$txdOvw"
		echo "        <th>Alpha texture</th>" >> "$txdOvw"
		echo "        <th>Bytes per pixel</th>" >> "$txdOvw"
		echo "        <th>Raster format</th>" >> "$txdOvw"
		echo "        <th>Converted image</th>" >> "$txdOvw"
		echo "      </tr>" >> "$txdOvw"

		for texture in `cat "$texdataFile"`
		do
			texname=`echo $texture | cut -d, -f1 | tr '%' ' '`
			escapedTexname=`echo $texname | sed 's/&/&amp;/'`
			compression=`echo $texture | cut -d, -f2`
			format=`echo $texture | cut -d, -f7`

			formatCounts[$format]=$(( ${formatCounts[$format]} + 1 ))
			compressionCounts[$compression]=$(( ${compressionCounts[$compression]} + 1 ))

			#echo '      <tr bgcolor="#00BFFF" align="center">' >> "$txdOvw"
			#echo $texture | awk -F, 'BEGIN {RS=","} {gsub("\n", ""); gsub("&", "&amp;"); print "        <td>" $0 "</td>"}' >> "$txdOvw"
			#echo "        <td><a href=\"$escapedTexname.png\"><img src=\"$escapedTexname.$fileFormat\" width=\"128\" height=\"128\" /></a></td>" >> "$txdOvw"
			#echo "      </tr>" >> "$txdOvw"

            if [ "$3" != "false" ]
            then
			    echo "  Extracting texture '$texname'"
			    gtatxd x "$tmpTxdfile" "$texname" "$2/$destdir/$texname.png" > /dev/null

                if [ $? -ne 0 ]
                then
                    echo "*** Error 3, aborting."
                    exit $?
                fi
            fi
		done

		echo "    </table>" >> "$txdOvw"
		
		echo '  </body>' >> "$txdOvw"
		echo '</html>' >> "$txdOvw"
	fi

	rm "$tmpTxdfile"
done

echo '    </table>' >> $ovw

rm "$texdataFile"

echo >> $ovw

for (( i = 0 ; i < 5 ; i++ ))
do
	echo "    <br/>" >> $ovw
done

echo >> $ovw
echo >> $ovw



echo "    <h2>Statistics</h2>" >> $ovw
echo >> $ovw


echo "    <h3>Raster formats</h3>" >> $ovw
echo '    <table border="1">' >> $ovw
echo '      <tr bgcolor="#4682B4">' >> $ovw
echo '        <th>Format</th>' >> $ovw
echo '        <th>Count</th>' >> $ovw
echo '      </tr>' >> $ovw

for format in ${!formatCounts[@]}
do
	echo '      <tr bgcolor="#00BFFF">' >> $ovw
	echo "        <td>$format</td>" >> $ovw
	echo "        <td>${formatCounts[$format]}</td>" >> $ovw
	echo '      </tr>' >> $ovw
done

echo '    </table>' >> $ovw
echo >> $ovw

echo "    <br/>" >> $ovw
echo "    <br/>" >> $ovw


echo "    <h3>Compression</h3>" >> $ovw
echo '    <table border="1">' >> $ovw
echo '      <tr bgcolor="#4682B4">' >> $ovw
echo '        <th>Compression</th>' >> $ovw
echo '        <th>Count</th>' >> $ovw
echo '      </tr>' >> $ovw

for compression in ${!compressionCounts[@]}
do
	echo '      <tr bgcolor="#00BFFF">' >> $ovw
	echo "        <td>$compression</td>" >> $ovw
	echo "        <td>${compressionCounts[$compression]}</td>" >> $ovw
	echo '      </tr>' >> $ovw
done

echo '    </table>' >> $ovw
echo >> $ovw


echo "  </body>" >> $ovw
echo "</html>" >> $ovw

